package com.yms.aop;

import com.yms.aop.service.impl.LoginServiceImpl;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @version : 1.0
 * @author: yangmingsong
 * @date: 2022/8/7-08-07-15:16
 * @description: spring5-test
 */
public class LoginProxy implements InvocationHandler {

    private Object proxy;

    public LoginProxy(){
        this.proxy = new LoginServiceImpl();
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        String pointCut = "com.yms.aop.service.LoginService";
//        如果需要增强的方法是我们定义的，那么根据方法所添加的注解来判断是前置还是后置通知
        int before = 0;
        int after = 0;
        System.out.println(proxy.getClass().getName());
        System.out.println(method.getName());
        if (method.getName().contains(pointCut)) {
            Annotation[] annotations = method.getAnnotations();
            for (Annotation annotation : annotations){
                String name = annotation.annotationType().getName();
                System.out.println(name);
//                并且规定前置通知和后置通知只能出现在一个方法上
                if("com.yms.aop.innotion.Before".equals(name)){
                    //前置通知
                    before++;
                    System.out.println("方法执行之前");
                }else if("com.yms.aop.innotion.After".equals(name)){
                    after++;
                    System.out.println("方法执行之后");
                }
            }
        }
        Object invoke = method.invoke(proxy, args);


        return invoke;
    }
}
